package wli.hibernate.util;

import java.util.List;
import java.util.Iterator;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import wli.hibernate.util.HibernateUtil;
import wli.hibernate.java.*;

public class DatabaseInstall {

	public Long saveCategory()

	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;

		Long courseId = null;

		try {

			transaction = session.beginTransaction();

			Category cat = new Category();
			cat.setId(1);
			cat.setName("ALL");

			transaction.commit();

		} catch (HibernateException e) {

			transaction.rollback();

			e.printStackTrace();

		} finally {

			session.close();

		}

		return courseId;

	}
/*
	public void listCourse() 
		   
		{ 
		      
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		 
		Transaction transaction = null; 
		  
		try { 
		     
		transaction = session.beginTransaction(); 
		  
		List courses = session.createQuery("from Course").list(); 
		    
		for (Iterator iterator = courses.iterator(); iterator.hasNext();) 
		   
		{ 
		      
		Course course = (Course) iterator.next(); 
		   
		System.out.println(course.getCourseName()); 

		transaction.commit(); 
		        
		} catch (HibernateException e) { 
		            
		transaction.rollback(); 
		            
		e.printStackTrace(); 
		        
		} finally { 
		            
		session.close(); 
		        
		} 
		    
		}	public void updateCourse(Long courseId, String courseName)

	{

		Session session = HibernateUtil.getSessionFactory().openSession();

		Transaction transaction = null;

		try {

			transaction = session.beginTransaction();

			Course course = (Course) session.get(Course.class, courseId);

			course.setCourseName(courseName);

			transaction.commit();

		} catch (HibernateException e) {

			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	public void deleteCourse(Long courseId) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			Course course = (Course) session.get(Course.class, courseId);
			session.delete(course);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
*/
}
